package com.jianguo.web.controller.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jianguo.common.core.domain.AjaxResult;
import com.jianguo.order.domain.PzGroup;
import com.jianguo.order.domain.PzGroupMenber;
import com.jianguo.order.domain.PzGroupMsg;
import com.jianguo.order.domain.PzOrderStepRecord;
import com.jianguo.order.service.IPzGroupMenberService;
import com.jianguo.order.service.IPzGroupMsgService;
import com.jianguo.order.service.IPzGroupService;
import com.jianguo.order.service.IPzOrderStepRecordService;
import com.jianguo.user.domain.PzUser;
import com.jianguo.user.service.IPzUserService;
import liquibase.pro.packaged.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/group")
public class GroupApi {
    @Autowired
    private IPzUserService pzUserService;
    @Autowired
    private IPzGroupMsgService pzGroupMsgService;
    @Autowired
    private IPzGroupMenberService pzGroupMenberService;
    @Autowired
    private IPzGroupService pzGroupService;
    @Autowired
    private IPzOrderStepRecordService pzOrderStepRecordService;

    // 根据订单id获取群组信息
    @GetMapping("/get")
    public AjaxResult get(String orderId) {
        PzGroup group = pzGroupService.getOne(new LambdaQueryWrapper<PzGroup>()
                .eq(PzGroup::getOrderId, orderId));
        if (ObjectUtil.isNull(group)) {
            return AjaxResult.error("订单不存在");
        }
        String groupId = group.getId();
        // 获取群组消息
        List<PzGroupMsg> msgList = pzGroupMsgService.list(new LambdaQueryWrapper<PzGroupMsg>()
                .eq(PzGroupMsg::getGroupId, groupId).orderByAsc(PzGroupMsg::getCreateTime));
        // 获取群组成员
        return AjaxResult.success(msgList);
    }
    // 发送群组消息
    @GetMapping("/send")
    public AjaxResult send(String orderId, String msg, HttpServletRequest request) {
        String account = request.getHeader("account");
        PzGroupMsg groupMsg = new PzGroupMsg();
        // 根据订单id获取群组信息
        PzGroup group = pzGroupService.getOne(new LambdaQueryWrapper<PzGroup>()
                .eq(PzGroup::getOrderId, orderId));
        if (ObjectUtil.isNull(group)) {
            return AjaxResult.error("订单不存在");
        }
        // 根据账号获取客户信息
        PzUser user = pzUserService.getOne(new LambdaQueryWrapper<PzUser>()
                .eq(PzUser::getAccount, account));
        if (ObjectUtil.isNull(user)) {
            return AjaxResult.error("账号不存在");
        }
        groupMsg.setUserId(user.getId());
        groupMsg.setMsgType(1L);
        groupMsg.setUserAccount(user.getAccount());
        groupMsg.setGroupId(group.getId());
        groupMsg.setMsg(msg);
        groupMsg.setPic(user.getPic());
        pzGroupMsgService.save(groupMsg);
        return AjaxResult.success();
    }

    // 获取群组列表
    @GetMapping("/list")
    public AjaxResult list(String account) {
        PzUser user = pzUserService.getOne(new LambdaQueryWrapper<PzUser>()
                .eq(PzUser::getAccount, account));
        if (ObjectUtil.isNull(user)) {
            return AjaxResult.error("账号不存在");
        }
        List<PzGroupMenber> groupIdList = pzGroupMenberService.list(new LambdaQueryWrapper<PzGroupMenber>()
                .eq(PzGroupMenber::getUserId, user.getId()));
        if (ObjectUtil.isEmpty(groupIdList)) {
            return AjaxResult.success();
        }
        List<PzGroup> groupList = pzGroupService.list(new LambdaQueryWrapper<PzGroup>()
                .in(PzGroup::getId, groupIdList.stream().map(PzGroupMenber::getGroupId).toArray()));
        return AjaxResult.success(groupList);
    }

    // 获取订单进度
    @GetMapping("/progress")
    public AjaxResult progress(String orderId) {
        PzOrderStepRecord one = pzOrderStepRecordService.getOne(new LambdaQueryWrapper<PzOrderStepRecord>()
                .eq(PzOrderStepRecord::getOrderId, orderId)
                .eq(PzOrderStepRecord::getStatus, 1));
        if (ObjectUtil.isNull(one)) {
            return AjaxResult.success("待开始...");
        }
        return AjaxResult.success(one.getName());
    }
}
